﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class CompareValidator
    {
        public static bool Compare(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {

            return Validate(rule, Field);
        }

        private static bool Validate(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                string[] ValidationData = rule.ValidateAgainst.Split('|');

                SPDynamicField Field2 = Field.FieldCollection.SingleOrDefault(hd => hd.InternalName == ValidationData[1]);
                if (Field2 == null)
                {
                    rule.ValidationMessage = "Unable to locate field " + ValidationData[1] + ".";
                    rule.IsValid = false;
                    return false;
                }
                //string ErrorMsg = string.Empty;
                //SPDynamicFieldValidationRule rl = new SPDynamicFieldValidationRule();
                //rl.ValidateAgainst = Field2.Value.ToString();
                rule.ValidateAgainst = Field2.Value.ToString();
                switch (ValidationData[0])
                {
                    case ">":
                        rule.ValidationMessage = Field.DisplayName + " must be greater than " + Field2.DisplayName + ".";

                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.GreaterThan(rule, Field);
                        }
                        else
                        {
                            return Validation.NumericValidator.GreaterThan(rule, Field);
                        }
                    case ">=":
                        rule.ValidationMessage = Field.DisplayName + " must be greater than equal to " + Field2.DisplayName + ".";

                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.GreaterThanEqual(rule, Field);
                        }
                        else
                        {
                            return Validation.NumericValidator.GreaterThanEqual(rule, Field);
                        }
                    case "<":
                        rule.ValidationMessage = Field.DisplayName + " must be less than " + Field2.DisplayName + ".";
                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.LessThan(rule, Field);
                        }
                        else
                        {
                            return Validation.NumericValidator.LessThan(rule, Field);
                        }
                    case "<=":
                        rule.ValidationMessage = Field.DisplayName + " must be greater than or equal to " + Field2.DisplayName + ".";
                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.LessThanEqual(rule, Field);
                        }
                        else
                        {
                            return Validation.NumericValidator.LessThanEqual(rule, Field);
                        }
                    case "=":
                        rule.ValidationMessage = Field.DisplayName + " must be equal to " + Field2.DisplayName + ".";
                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.Equal(rule, Field);
                        }
                        //else
                        //{
                        //    return Validation.NumericValidator.Equal(rule, Field);
                        //}

                        else if (Field.FieldType == "Number" || Field.FieldType == "Integer" || Field.FieldType == "Currency")
                        {
                            return Validation.NumericValidator.Equal(rule, Field);
                        }
                        else
                        {
                            return Validation.StringValidator.Equal(rule, Field);
                        }
                    case "!=":
                        rule.ValidationMessage = Field.DisplayName + " must not be equal to " + Field2.DisplayName + ".";
                        if (Field.FieldType == "DateTime")
                        {
                            return Validation.DateValidator.NotEqual(rule, Field);
                        }
                        //else
                        //{
                        //    return Validation.NumericValidator.NotEqual(rule, Field);
                        //}
                        else if (Field.FieldType == "Number" || Field.FieldType == "Integer" || Field.FieldType == "Currency")
                        {
                            return Validation.NumericValidator.NotEqual(rule, Field);
                        }
                        else
                        {
                            return Validation.StringValidator.NotEqual(rule, Field);
                        }
                }
                rule.IsValid = true;
                return true;
            }
            catch
            {
                rule.IsValid = false;
                rule.ValidationMessage = "Unable to perform a comparision.";
                return false;
            }
        }

    }
}
